package com.isorf.controller;


import com.github.pagehelper.PageInfo;
import com.isorf.pojo.brand;
import com.isorf.pojo.color;
import com.isorf.pojo.links;
import com.isorf.pojo.student;
import com.isorf.service.Linkservice;
import com.isorf.service.UploadService;
import com.isorf.service.colorservice;
import com.isorf.util.responseresult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class colorcontroller {
    @Autowired
    private colorservice colorservice;
    @Autowired
    private UploadService uploadService;
    @Value("${file.upload.path}")     // 从application.yml文件中获取图片上传的路径
    private String path;


    @PostMapping("/updatecolor")
    public responseresult<Boolean> updatecolor(@RequestBody color color) {
        if (color == null || color.getColorId() == null) {
            return responseresult.failure(responseresult.FAILED, "更新失败，缺少必要参数");
        }
        Boolean result = colorservice.updatecolor(color);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "更新成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "更新失败");
        }
    }


    @PostMapping("/addcolor")
    public responseresult<Boolean> addcolor(@RequestBody color color) {
        Boolean result = colorservice.addcolor(color);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "Link successful", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "Link failed");
        }
        // Remove the unreachable statement
    }

    @PostMapping("/paggggeList")
    public responseresult<PageInfo<color>> pageList(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {
        PageInfo<color> result = colorservice.pageList(pageNum, pageSize);
        if (result==null&&result.getList().size()==0) {
            return responseresult.failure(responseresult.FAILED, "查询用户列表失败");
        }else {
            return responseresult.success(responseresult.SUCCESS,"查询成功", result);
        }
    }

    @PostMapping("/batchDeeeelete")
    public responseresult<Boolean> batchDelete(@RequestBody List<Integer> ids) {
        // 检查参数
        if (ids == null || ids.isEmpty()) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }

        // 调用service层的批量删除方法
        Boolean result = colorservice.batchDelete(ids);
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "批量删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "批量删除失败");
        }
    }

    @PostMapping("/deletecolor")
    public responseresult<Boolean> deletecolor(@RequestBody color color) {
        // 检查link对象和id是否为空
        if (color == null || color.getColorId() == null) {
            return responseresult.failure(responseresult.FAILED, "删除失败，缺少必要参数");
        }

        // 调用service层的删除方法，应该是一个删除方法而不是addlink
        Boolean result = colorservice.deletecolor(color.getColorId());
        if (result) {
            return responseresult.success(responseresult.SUCCESS, "删除成功", result);
        } else {
            return responseresult.failure(responseresult.FAILED, "删除失败");
        }
    }


    @GetMapping("/getallcolor")
    public responseresult<List<color>> getallcolor() {
        List<color> color = colorservice.getallcolor();
        if (color == null) {
            return responseresult.failure(508, "没有学生信息");
        }else {
            return responseresult.success(200, "查询成功", color);

        }
    }




}
